package com.fieldchain.ex.fa100.ui.c2c.activity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.C2CSellBean;
import com.fieldchain.ex.fa100.ui.personal.activity.BankActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.BeanRequest;
import com.fieldchain.ex.fa100.utils.net.modle.StringRequest;
import com.fieldchain.ex.fa100.utils.screen.MyToast;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/18
 */
public class C2CSellDetailsActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private TextView tvName;
    private TextView tvBankname;
    private TextView tvChannel;
    private LinearLayout linChosebank;
    private ImageView ivChose;
    private TextView tvBankid;
    private TextView tvCopy1;
    private TextView tvNum;
    private TextView tvStatus;
    private TextView tvSure;
    private TextView tvDelete;


    private List<C2CSellBean.UserCardBean> list = new ArrayList<>();
    private String[] str = {"未确认", "已确认", "已取消", "已审核", "已驳回", "已过期"};
    private C2CSellBean bean;
    private int id;
    private int bankId;
    private C2CSellBean.UserCardBean fBean;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        tvName = findViewById(R.id.tv_name);
        tvBankname = findViewById(R.id.tv_bankname);
        tvChannel = findViewById(R.id.tv_channel);
        linChosebank = findViewById(R.id.lin_chosebank);
        ivChose = findViewById(R.id.iv_chose);
        tvBankid = findViewById(R.id.tv_bankid);
        tvCopy1 = findViewById(R.id.tv_copy1);
        tvNum = findViewById(R.id.tv_num);
        tvStatus = findViewById(R.id.tv_status);
        tvSure = findViewById(R.id.tv_sure);
        tvDelete = findViewById(R.id.tv_delete);
    }

    @Override
    public void setValue() {
        id = getIntent().getIntExtra("id", 0);
        tvCenter.setText("订单详情");
        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        tvCopy1.setOnClickListener(this);
        linChosebank.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    @Override
    public int getLayout() {
        return R.layout.activity_c2cselldetails;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.tv_sure:
                sure();
                break;
            case R.id.tv_copy1:
                copy(tvBankid);
                break;
            case R.id.lin_chosebank:
                Intent intent = new Intent(mContext, BankActivity.class);
                intent.putExtra("type", 5);
                startActivity(intent);
                break;
            case R.id.tv_delete:
                delete();
                break;
        }
    }

    public void copy(TextView tv) {
        ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        // 将文本内容放到系统剪贴板里。
        ClipData clipData = ClipData.newPlainText("", tv.getText().toString());
        cm.setPrimaryClip(clipData);
        MyToast.show(this, "复制成功");
    }

    public void getData() {
        String url = Constant.base + "exchanges/" + id;
        HttpRequest.getInstance().getRequest(url, new BeanRequest<C2CSellBean>() {
            @Override
            public void onSucess(C2CSellBean bean, int code, String msg) {
                if (bean != null) {
                    list.addAll(bean.getUserCard());
                    fBean = list.get(0);
                    bankId = fBean.getId();
                    tvName.setText(fBean.getHolder());
                    tvBankname.setText(fBean.getBank());
                    tvChannel.setText(fBean.getBranch());
                    tvBankid.setText(fBean.getBankCard());
                    tvNum.setText(bean.getInfo().getAmount() + "");
                    int status = bean.getInfo().getStatus();
                    tvStatus.setText(str[status]);
                    if (status > 0) {
                        linChosebank.setClickable(false);
                        tvSure.setVisibility(View.GONE);
                        tvDelete.setVisibility(View.GONE);
                    } else {
                        linChosebank.setClickable(true);
                        tvSure.setVisibility(View.VISIBLE);
                        tvDelete.setVisibility(View.VISIBLE);
                    }

                }
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public void sure() {
        String url = Constant.base + "/exchanges/withdraw/" + id;
        HashMap map = new HashMap();
        map.put("orderId", id + "");
        map.put("cardId", bankId + "");
        HttpRequest.getInstance().postRequest(map, url, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                if (code == 200) {
                    mContext.startActivity(new Intent(mContext, C2CHistoryActivity.class));
                    finish();
                }
                MyToast.show(msg);
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    public void delete() {
        HashMap map = new HashMap();
        map.put("orderId", id + "");
        String url = Constant.c2cDelete + id;
        HttpRequest.getInstance().postRequest(map, url, new StringRequest() {
            @Override
            public void onSucess(String str, int code, String msg) {
                MyToast.show(msg+"");
                if(code==200)finish();
            }

            @Override
            public void onFaild(Exception e) {

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 5) {
            bankId = data.getIntExtra("id", 0);
        }
    }
}
